// 简单测试AI解析API
console.log('🧪 测试AI智能解析功能...')

// 检查环境变量
console.log('🔧 检查环境配置:')
console.log('OPENAI_API_KEY:', process.env.OPENAI_API_KEY ? '***已设置***' : '❌未设置')
console.log('OPENAI_BASE_URL:', process.env.OPENAI_BASE_URL || '❌未设置')
console.log('OPENAI_MODEL:', process.env.OPENAI_MODEL || '❌未设置')

// 测试API调用
const testAPI = async () => {
  try {
    const http = require('http')
    
    const postData = JSON.stringify({
      text: '今天下午1点15分开财务现金流讨论会'
    })
    
    const options = {
      hostname: 'localhost',
      port: 5000,
      path: '/api/ai/smart-parse',
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'Content-Length': Buffer.byteLength(postData)
      }
    }
    
    return new Promise((resolve, reject) => {
      const req = http.request(options, (res) => {
        let data = ''
        
        res.on('data', (chunk) => {
          data += chunk
        })
        
        res.on('end', () => {
          console.log('\n📊 API响应状态:', res.statusCode)
          console.log('📝 响应内容:', data)
          resolve(data)
        })
      })
      
      req.on('error', (err) => {
        console.log('\n❌ API调用失败:', err.message)
        if (err.code === 'ECONNREFUSED') {
          console.log('💡 后端服务可能未启动，请运行: npm run dev')
        }
        reject(err)
      })
      
      req.write(postData)
      req.end()
    })
  } catch (error) {
    console.log('❌ 测试出错:', error.message)
  }
}

// 执行测试
testAPI()
